package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.isunland.managesystem.adapter.CompanyForumTypeAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CompanyForumType;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 对象功能:  农技服务二级分类列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/16 14:35
 */
public class AgricultureServiceSecondFragment extends BaseListFragment {
  protected static final String EXTRA_VALUE =
      AgricultureServiceSecondFragment.class.getSimpleName() + ".EXTRA_VALUE";
  protected static final String EXTRA_FFROM =
      AgricultureServiceSecondFragment.class.getSimpleName() + ".EXTRA_FROM";
  protected static final String ALL = "all";//农作物圈全部二级分类
  protected static final String COLLECT = "collect";//首页分类的二级分类
  protected static final String QUERY = "query";//一级点击搜索跳转到二级
  private CompanyForumTypeAdapter mAdapter;
  private CompanyForumType mContent;
  private String mFrom;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContent = (CompanyForumType) getArguments().getSerializable(EXTRA_VALUE);
    mFrom = getArguments().getString(EXTRA_FFROM);
    setBack(true);
    setTitleCustom(mContent.getName());
  }

  @Override public String getUrl() {
    if (ALL.equalsIgnoreCase(mFrom) || QUERY.equalsIgnoreCase(mFrom)) {
      return ApiConst.URL_SECOND_LIST;
    } else if (COLLECT.equalsIgnoreCase(mFrom)) {
      return ApiConst.URL_SECOND_MY_LIST;
    } else {
      return "";
    }
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty mParams = new ParamsNotEmpty();
    if (QUERY.equalsIgnoreCase(mFrom)) {
      mParams.put("cropType", mContent.getName());
    } else {
      mParams.put("pid", mContent.getId());
    }
    return mParams.getParams();
  }

  @Override public void resolveResponse(String arg0) {
    Type listType = new TypeToken<ArrayList<CompanyForumType>>() {
    }.getType();
    List<CompanyForumType> mForumType =
        new Gson().<ArrayList<CompanyForumType>>fromJson(arg0, listType);
    setAdapter(mForumType);
  }

  private void setAdapter(List<CompanyForumType> forumType) {
    if (forumType == null) {
      forumType = new ArrayList<>();
    }
    mAdapter = new CompanyForumTypeAdapter(getActivity(), forumType);
    setListAdapter(mAdapter);
    ((CompanyForumTypeAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    CompanyForumType mCompanyForumType = mAdapter.getItem(position - 1);
    Intent intent = new Intent(getActivity(), AgriculturalForumPagerActivity.class);
    intent.putExtra(AgriculturalForumListFragment.EXTRA_VALUE, mCompanyForumType);
    if (ALL.equalsIgnoreCase(mFrom)) {
      intent.putExtra(AgriculturalForumListFragment.EXTRA_FROM, AgriculturalForumListFragment.TYPE);
    } else if (COLLECT.equalsIgnoreCase(mFrom)) {
      intent.putExtra(AgriculturalForumListFragment.EXTRA_FROM,
          AgriculturalForumListFragment.COLLECT_TYPE);
    } else {
      intent.putExtra(AgriculturalForumListFragment.EXTRA_FROM, AgriculturalForumListFragment.TYPE);
    }
    startActivityForResult(intent, 0);
    super.onListItemClick(l, v, position, id);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮
      case android.R.id.home:

        getActivity().finish();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;

    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static Fragment newInstance(CompanyForumType content, String from) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VALUE, content);
    bundle.putSerializable(EXTRA_FFROM, from);
    AgricultureServiceSecondFragment fragment = new AgricultureServiceSecondFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
